package com.ews.onlineexchange.controller;
import com.ews.onlineexchange.model.CodeDic;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.service.CodeDicService;
import com.ews.onlineexchange.service.EntityHome;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class CodeDicHome extends EntityHome<CodeDic, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	CodeDicService codeDicService;
	private String tempCodeName = "";
	private String tempCodeValue = "";
	
	public String getTempCodeName() {
		return tempCodeName;
	}
	public void setTempCodeName(String tempCodeName) {
		this.tempCodeName = tempCodeName;
	}
	public String getTempCodeValue() {
		return tempCodeValue;
	}
	public void setTempCodeValue(String tempCodeValue) {
		this.tempCodeValue = tempCodeValue;
	}
	
	
	@Override
	public CodeDic loadInstance() {
		CodeDic instance = super.loadInstance();
		setTempCodeName(instance.getCodeName());
		setTempCodeValue(instance.getCodeValue());
		return instance;
	}
	@Override
	public String saveAndFlush() {
		String codeName = getInstance().getCodeName().trim();
		String codeVlaue = getInstance().getCodeValue().trim();
		String codeType = getInstance().getCodeType();
		if(!tempCodeName.equals(codeName) && !tempCodeValue.equals(codeVlaue)) {
			if(codeDicService.isExistCodeNameAndCodeValue(codeName, codeVlaue,codeType)) {
				FacesContext.getCurrentInstance().addMessage("codeName",
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "此类别下代码名称或代码值已存在！", ""));
				return null;
			}
		}
		String result = super.saveAndFlush();
		if("saved".equals(result))return "CodeDicList.xhtml?faces-redirect=true&amp;includeViewParams=true";
		else return null;
	}
	@Override
	public String delete() {
		
		getInstance().setDelFlag(DataEntity.DEL_FLAG_DELETE);
		return super.saveAndFlush();
	}
	
	
	

	
	
}
